<?php
namespace app\index\controller\project;

use app\index\controller\Base;

class Company extends base_project
{
    public $tableName = "Company";
    public function index()
    {
        $company_cat=  model("CodeMap")->get_value("company_cat");
         //单位职务
         $company_job=    model("CodeMap")->get_value("company_job");
       $data["company_cat"] = array_column($company_cat,"m_name","m_value");
     //  $data["company_job"] =    array_column($company_job,"m_name","m_value");
     
       $this->assign($data);
        $this->page();
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
        $model = model('Company');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
         
            $data["name"] = $post["name"];
            $data["c_type"] = $post["c_type"];
            $data["contact"] = $post["contact"];
            $data["position"] = $post["position"];
            $data["remark"] = $post["remark"];
       
            $data["status"] =1;
            $data["phone"] = $post["phone"];

            $CompanyModel = model("Company");
            $result = $CompanyModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Company/index"));
        }
        //单位类型
       $data["company_cat"]=  model("CodeMap")->get_value("company_cat");
         //单位职务
         $data["company_job"]=    model("CodeMap")->get_value("company_job");
         $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $CompanyModel = model('Company');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
           
            $data["name"] = $post["name"];
            $data["c_type"] = $post["c_type"];
            $data["contact"] = $post["contact"];
            $data["position"] = $post["position"];
            $data["remark"] = $post["remark"];
            $data["status"] =1;
          
            $data["phone"] = $post["phone"];

            $map["id"] = $post["id"];
            $result = $CompanyModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Company/index"));
        }

        $id = input("id");
        //单位类型
        $data["company_cat"]=  model("CodeMap")->get_value("company_cat");
        //单位职务
        $data["company_job"]=    model("CodeMap")->get_value("company_job");
        $data["info"] = $CompanyModel->get($id);

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $CompanyModel = model('Company');
        $result = $CompanyModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

}
